package dyyx;

import java.io.StringWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;

import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.VelocityEngine;

public class VelocityDemo {

	public static void main(String[] args) throws Exception {

		Properties properties = new Properties();
		properties.setProperty("resource.loader", "class");
		properties.setProperty("class.resource.loader.class",
				"org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader");
		VelocityEngine velocityEngine = new VelocityEngine(properties);
		
		VelocityContext context = new VelocityContext();
		context.put("name", "VELOCITY");
		
		String[] strList = {"abc","xyz"};
		context.put("strList", strList);
		
		List<Map<String,Object>> userList = new ArrayList<>();
		Map<String,Object> map = new HashMap<>();
		map.put("id", 1);
		map.put("name", "dyyx");
		userList.add(map);
		
		map = new HashMap<>();
		map.put("id", 2);
		map.put("name", "tiger");
		userList.add(map);
		
		context.put("userList", userList);
		
		StringWriter writer = new StringWriter();
		
		velocityEngine.mergeTemplate("template/hello.vm", "utf8", context, writer);

		System.out.println(writer);

		

	}
}
